﻿using System;

using System.Configuration;
using System.Collections.Generic;


namespace TeamDeploy
{
    class Program
    {
        static void Main(string[] args)
        {
            string argZero = null;
            
            try
            {
                if (args.Length == 0 || args[0] == "/?" || args[0] == "-?")
                {
                    DisplayHelp();
                    return;
                }

                argZero = args[0].Substring(1, args[0].IndexOf(":") - 1);
                TeamDeploy.Tasks.Deploy deploy = new TeamDeploy.Tasks.Deploy();
                switch (argZero.ToUpper())
                {
                    case "CONFIG":
                        // Load config file
                        string File = args[0].Substring(args[0].IndexOf(":") + 1);

                        // Use EXE folder path if one is not specified.
                        if (File.IndexOf("\\") < 0)
                        {
                            File = System.IO.Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, File);
                        }
                        deploy.DeployScript = File.ToString();
                        deploy.KillAppPathFilename = ConfigurationManager.AppSettings["KillAppPathFilename"].ToString();
                        deploy.RemoteExecutePathFilename = ConfigurationManager.AppSettings["RemoteExecutePathFilename"].ToString();
                        deploy.BuildEngine = new MockBuild();
                        deploy.Execute();
                        break;

                    case "GETPRODUCTCODE":
                        string productName = args[0].Substring(args[0].IndexOf(":") + 1);

                        List<string> codes = deploy.GetUninstallProductCodes(productName, Environment.MachineName);

                        if (codes != null && codes.Count > 0)
                        {
                            Console.WriteLine(codes.Count + " Product(s) found.");
                            foreach (string code in codes)
                            {
                                Console.WriteLine(code);
                            }
                        }
                        else
                        {
                            Console.WriteLine("Product not found.");
                        }

                        break;
                }

            }
            catch (Exception ex)
            {

                Console.WriteLine(ex.ToString());

            }

        }
        private static void DisplayHelp()
        {
            Console.WriteLine("This utility installs MSIs on target machines.");
            Console.WriteLine("");
            Console.WriteLine("TeamDeploy [/CONFIG:file1]");
            Console.WriteLine("");
            Console.WriteLine("/CONFIG:file1    Specifies XML Config file containing the required and ");
            Console.WriteLine("                 optional options to install MSIs.");
        }
    }
}
